package com.ruoyi.web.cp.service;

import com.ruoyi.web.core.config.ConfigLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Slf4j
@Service
public class GitService {
    @Autowired
    private ConfigLoader configLoader;

    public void gitPush() {
        ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", configLoader.getGamenamesPath() + "git-pull.bat");
        processBuilder.redirectErrorStream(true);

        try {
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                log.info(line);
            }
            int exitCode = process.waitFor();
            log.info("Exited with code: " + exitCode);
        } catch (IOException | InterruptedException e) {
            log.error(e.getMessage(), e);
        }
    }

    public void gitPull() {
        ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", configLoader.getGamenamesPath() + "git-pull.bat");
        processBuilder.redirectErrorStream(true);

        try {
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                log.info(line);
            }
            int exitCode = process.waitFor();
            log.info("Exited with code: " + exitCode);
        } catch (IOException | InterruptedException e) {
            log.error(e.getMessage(), e);
        }
    }
}
